<?php
namespace app\material\model;
use app\BaseModel;
use think\facade\Db;



class FileCollect extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_material_file_collect';


    /**
     * @author chenniang
     * @DataTime: 2021-07-05 17:40
     * @功能说明:
     */
    public function getCreateTimeAttr($value,$data){

        if(!empty($value)){

            return date('Y-m-d H:i:s',$value);
        }


    }



    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:00
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        $res = $this->insert($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:03
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        if(isset($data['company'])){

            $company = $data['company'];

            unset($data['company']);

            $this->updateSome($dis['id'],$data['uniacid'],$company);
        }

        $res = $this->where($dis)->update($data);

        return $res;

    }

    /**
     * @author chenniang
     * @DataTime: 2021-06-24 15:53
     * @功能说明:添加公司
     */
    public function updateSome($id,$uniacid,$company){

        $company_model = new FileCompany();

        $company_model->where(['brochure_id'=>$id])->delete();

        $company = $company_model->changeData($company);

        if(!empty($company)){

            foreach ($company as $value){

                $insert = [

                    'uniacid' => $uniacid,

                    'file_id' => $id,

                    'company_id' => $value
                ];

                $company_model->dataAdd($insert);

            }

        }

        return true;


    }

    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:04
     * @功能说明:详情
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-07-09 15:48
     * @功能说明:收藏数量
     */
    public function collectCount($user_id){

        $dis = [

            'a.user_id' => $user_id,

            'b.status'  => 1
        ];
        $data = $this->alias('a')
            ->join('longbing_card_material_file b','b.id = a.file_id')
            ->where($dis)
            ->group('b.id')
            ->count();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:05
     * @功能说明:
     */
    public function dataList($dis,$page=10){

        $data = $this->alias('a')
            ->join('longbing_card_material_file b','b.id = a.file_id')
            ->where($dis)
            ->field('b.*')
            ->group('b.id')
            ->order('a.id desc')
            ->paginate($page)
            ->toArray();

        return $data;

    }





}